﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;               //USING MYSQL CONNECTOR

namespace WindowsFormsApplication1
{
    public partial class AddUser : Form
    {
        //Validation Initializations
        string allowed_username = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";//alpha numeric
        string allowed_alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//alpha
        string allowed_number = "0123456789";
        bool validation;
       
        public AddUser()
        {
            InitializeComponent();
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            validation = true;

            #region userid_validation
            if (this.textBox_userId.Text == null)
            {
                validation = false;
                this.textBoxUseridlabel.BackColor = Color.Red;
            }
            else
            {
                this.textBoxUseridlabel.BackColor = Color.Black;
            }
            #endregion

            #region UserName Validation
            //UserName Validation
            /*
             * UserName Should Not Be Blank
             * UserName Should only contain AlpaNumeric Characters
             * */
            if (textBox_userName.Text.Length == 0)
            {
                MessageBox.Show("Username Should notbe Empty");
                validation = false;
                textBox_username_label.BackColor = Color.Red;
            }

            for (int i = 0; i < Int32.Parse(textBox_userName.Text.Length.ToString()); i++)
            {
                if (!(allowed_username.Contains(textBox_userName.Text[i].ToString())))
                {
                    MessageBox.Show("Username Should be AlphaNumeric");
                    textBox_userName.Text = "";
                    validation = false;
                    textBox_username_label.BackColor = Color.Red;
                }
            }

            # endregion

            #region password_validation

            if (this.textBox_password.Text == null)
            {
                validation = false;
                this.textBox_passwordLabel.BackColor = Color.Red;
            }
            else
            {
                this.textBox_passwordLabel.BackColor = Color.Black;
            }

            #endregion

            #region FNAME Validation
            //FNAME VALIDATION
            if (textBox_fName.Text.Length == 0)
            {
                MessageBox.Show("FirstName notbe Empty");
                validation = false;
                textBox_fNameLabel.BackColor = Color.Red;
            }

            for (int i = 0; i < Int32.Parse(textBox_fName.Text.Length.ToString()); i++)
            {
                if (!(allowed_alpha.Contains(this.textBox_fName.Text[i].ToString())))
                {
                    MessageBox.Show("First Name Should be Text Only");
                    textBox_fName.Text = "";
                    validation = false;
                    textBox_fNameLabel.BackColor = Color.Red;
                }

            }
            #endregion

            #region LNAME Validation
            if (textBox_lName.Text.Length == 0)
            {
                MessageBox.Show("Lastname Should not be Empty");
                validation = false;
                textBox_lNameLabel.BackColor = Color.Red;
            }
            //LNAME Validation
            for (int i = 0; i < Int32.Parse(textBox_lName.Text.Length.ToString()); i++)
            {
                if (!(allowed_alpha.Contains(this.textBox_lName.Text[i].ToString())))
                {
                    MessageBox.Show("Last Name Should be Text Only");
                    textBox_lName.Text = "";
                    textBox_lNameLabel.BackColor = Color.Red;
                    validation = false;
                }

            }
            #endregion

            #region ZIPCODE Validation
            //ZipCode Validation
            if (textBox_zipCode.Text.Length == 0)
            {
                MessageBox.Show("Zip Code Should notbe Empty");
                validation = false;
                textBox_zipCodeLabel.BackColor = Color.Red;
            }

            for (int i = 0; i < Int32.Parse(textBox_zipCode.Text.Length.ToString()); i++)
            {
                if (!(allowed_number.Contains(textBox_zipCode.Text[i].ToString())))
                {
                    MessageBox.Show("ZipCode should contain only numbers");
                    textBox_zipCode.Text = "";
                    textBox_zipCodeLabel.BackColor = Color.Red;
                    validation = false;
                }
            }
            #endregion

            #region TELEPHONE Validation

            //Telephone Validation
            if (textBox_telephone.Text.Length ==0)
            {
                MessageBox.Show("Telephone Should not be Empty");
                validation = false;
                textBox_telephoneLabel.BackColor = Color.Red;
            }
            for (int i = 0; i < Int32.Parse(textBox_telephone.Text.Length.ToString()); i++)
            {
                if (!(allowed_username.Contains(textBox_telephone.Text[i].ToString())))
                {
                    MessageBox.Show("Telephone should contain only numbers");
                    textBox_telephone.Text = "";
                    textBox_telephoneLabel.BackColor = Color.Red;
                    validation = false;
                }
            }
            #endregion

            #region address_validation
            if (this.textBox_address.Text == null)
            {
                validation = false;
                this.textBox_addressLabel.BackColor = Color.Red;
            }
            else
            {
                this.textBox_addressLabel.BackColor = Color.Black;
            }
            #endregion


            if (validation == true)
            {

            #region MYSQL Connection

                    //Variable Initializations
                    string dob = comboBox_year.SelectedItem.ToString() + "-" + ((int.Parse(comboBox_month.SelectedIndex.ToString())) + 1).ToString() + "-" + comboBox_day.SelectedItem.ToString();
                    //MYSQL Initializations
                    string connString = string.Format("server={0};port=3306;database={1};user id={2};password={3}",
                    tBox_server.Text, tBox_DbName.Text, tBox_username.Text, tBox_password.Text);
            
                    MySqlConnection conn = new MySqlConnection(connString);
                    MySqlDataReader reader = null;

                    try
                    {  conn = new MySqlConnection(connString);
                       conn.Open();
                    }
                    catch (MySqlException ex)
                    {
                        MessageBox.Show("Error: \n" + ex);
                    }
            #endregion

            string queryString = "INSERT INTO  `" + tBox_DbName.Text.ToString() + "`.`users` (`USERID` ,`USERNAME` ,`PASSWORD` ,`DOB` ,`FNAME` ,`LNAME` ,`ADDRESS` ,`STATE` ,`ZIP` ,`TELEPHONE` ,`EMAIL`)VALUES ('" + textBox_userId.Text.ToString() + "','" + textBox_userName.Text.ToString() + "','" + textBox_password.Text.ToString() + "','" + dob.ToString() + "','" + textBox_fName.Text.ToString() + "','" + textBox_lName.Text.ToString() + "','" + textBox_address.Text.ToString() + "','" + comboBox_state.SelectedItem.ToString() + "','" + textBox_zipCode.Text.ToString() + "','" + textBox_telephone.Text.ToString() + "','" + textBox_email.Text.ToString() + "');";
            MySqlCommand cmd = new MySqlCommand(queryString , conn);

            #region try_execution_command

            try
            {
            reader = cmd.ExecuteReader();
           
            }

           catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            finally
            {
                this.textBox_userId.Text = "";
                this.textBox_userName.Text = "";
                this.textBox_password.Text = "";
                this.textBox_fName.Text = "";
                this.textBox_lName.Text = "";
                this.textBox_telephone.Text = "";
                this.textBox_zipCode.Text = "";
                this.textBox_address.Text = "";
                this.textBox_email.Text = "";

                if (reader != null) reader.Close();
                conn.Close();
            }
            #endregion

            }//if_validation

            
        }                   
        
        private void button1_Click_1(object sender, EventArgs e)
        {
            this.textBox_fName.Text = "";
            this.textBox_lName.Text = "";
            this.textBox_password.Text = "";
            this.textBox_telephone.Text = "";
            this.textBox_userId.Text = "";
            this.textBox_zipCode.Text = "";
            this.textBox_address.Text = "";
            this.textBox_userName.Text = "";
            this.textBox_email.Text = "";
            this.comboBox_day.SelectedItem = null;
            this.comboBox_month.SelectedItem = null;
            this.comboBox_year.SelectedItem = null;
            this.comboBox_state.SelectedItem = null;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

                
        }

       
    }
